﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace WebPALTT.Lib
{
    /*
     * Class: ActionResults
     * 
     * This class contains the results of an action.
     */
    [Serializable()]
    public class ActionResults
    {
        TimeSpan mDuration;
        List<string> mErrors;
        List<string> mWarnings;
        List<object> mData;

        /*
         * Name: Properties
         * 
         * /TimeSpan/ Duration:
         * 
         * Gets or sets the time span the action took to complete.
         * 
         * /bool/ HasErrors:
         * 
         * Returns true if the Results contains any errors.
         * 
         * /bool/ HasWarnings:
         * 
         * Returns true if the Results contains any warnings.
         * 
         * /{{List<string>}}/ Error:
         * 
         * Gets the list of error from the results.
         * 
         * /{{List<string>}}/ Warnings:
         * 
         * Gets the list of warnings from the results.
         *
         * /{{List<object>}}/ Data:
         * 
         * Gets the list of data attached to the results. The data depends on the type of action.
         */

        public TimeSpan Duration { get { return mDuration; } set { mDuration = value; } }

        public bool HasErrors { get { return mErrors.Count > 0 ? true : false; } }

        public List<string> Errors { get { return mErrors; } }

        public bool HasWarnings { get { return mWarnings.Count > 0 ? true : false; } }

        public List<string> Warnings { get { return mWarnings; } }

        public List<object> Data { get { return mData; } }

        public ActionResults() : this(TimeSpan.Zero, null, null, null) { }

        public ActionResults(TimeSpan duration) : this(duration, null, null, null) { }

        public ActionResults(TimeSpan duration, object[] data) : this(duration, null, null, data) { }

        public ActionResults(TimeSpan duration, string[] errors, string[] warnings, object[] data)
        {
            mDuration = duration;
            mErrors = new List<string> { };
            mWarnings = new List<string> { };
            mData = new List<object> { };
            if (errors != null)
                mErrors.AddRange(errors);
            if (warnings != null)
                mWarnings.AddRange(warnings);
            if (data != null)
                mData.AddRange(data);
        }

        public ActionResults(SerializationInfo info, StreamingContext ctxt)
        {
            mDuration = (TimeSpan)info.GetValue("Duration", typeof(TimeSpan));
            mErrors = (List<string>)info.GetValue("Errors", typeof(List<string>));
            mWarnings = (List<string>)info.GetValue("Warnings", typeof(List<string>));
            mData = (List<object>)info.GetValue("Data", typeof(List<object>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Duration", mDuration);
            info.AddValue("Errors", mErrors);
            info.AddValue("Warnings", mWarnings);
            info.AddValue("Data", mData);
        }
    }
}
